unit EnemySetup;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, Mask, ExtCtrls, Menus, JvExMask, JvSpin, JvDBSpinEdit;

type
  TFormEnemySetup = class(TForm)
    BtnOK: TBitBtn;
    BtnCancel: TBitBtn;
    EditString: TEdit;
    List1: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    BtnDelete: TBitBtn;
    BtnAdd: TBitBtn;
    Bevel1: TBevel;
    Bevel2: TBevel;
    Memo2: TMemo;
    Bevel3: TBevel;
    Label4: TLabel;
    BtnActual: TBitBtn;
    Bevel4: TBevel;
    Bevel5: TBevel;
    MainMenu1: TMainMenu;
    MenuInvisible: TMenuItem;
    MenuESC: TMenuItem;
    EditValue: TJvDBSpinEdit;
    procedure BtnOKClick(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure EditValueExit(Sender: TObject);
    procedure BtnAddClick(Sender: TObject);
    procedure BtnDeleteClick(Sender: TObject);
    procedure EditStringKeyPress(Sender: TObject; var Key: Char);
    procedure EditValueKeyPress(Sender: TObject; var Key: Char);
    procedure BtnActualClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    actual, linked: boolean;
  end;

var
  FormEnemySetup: TFormEnemySetup;

implementation

{$R *.DFM}

procedure TFormEnemySetup.FormCreate(Sender: TObject);
begin
  linked:=false;
  actual:=false;
end;

procedure TFormEnemySetup.BtnOKClick(Sender: TObject);
begin
  if List1.items.count < 1 then begin
    messagedlg('You haven''t defined any String/Value cases',
      mtinformation,[mbok],0);
    EditString.selectall;
    EditString.setfocus;
    exit;
  end;
  linked:=true;
  close;
end;

procedure TFormEnemySetup.BtnCancelClick(Sender: TObject);
begin
  close;
end;

procedure TFormEnemySetup.EditValueExit(Sender: TObject);
begin
  with TJvDBSpinEdit(sender) do begin
    if value > 100 then value:=100;
    if value < 0 then value:=0;
  end;
end;

procedure TFormEnemySetup.BtnDeleteClick(Sender: TObject);
begin
  if List1.itemindex < 0 then exit;
  List1.items.delete(List1.itemindex);
end;

procedure TFormEnemySetup.EditStringKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    EditValue.setfocus;
  end;
end;

procedure TFormEnemySetup.EditValueKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnAdd.click;
  end;
end;

procedure TFormEnemySetup.BtnActualClick(Sender: TObject);
begin
  actual:=true;
  close;
end;

procedure TFormEnemySetup.BtnAddClick(Sender: TObject);
var
  str, val: string;
  checks: TStringList;
  i, p: integer;
  strfound, valfound: boolean;
begin
  if List1.Items.count >= 10 then begin
    messagedlg('You can only define 10 different cases here',
      mtinformation,[mbok],0);
    BtnDelete.setfocus;
    exit;
  end;
  str:=EditString.text;
  val:=inttostr(trunc(EditValue.value));
  if (pos('=',str) > 0) or (pos('@',str) > 0) or (pos(',',str) > 0) then begin
    messagedlg('You can''t have "=" "@" or "," in the String value',
      mtinformation,[mbok],0);
    EditString.selectall;
    EditString.setfocus;
    exit;
  end;
  checks:=TStringList.create;
  checks.assign(List1.items);
  strfound:=false;
  valfound:=false;
  for i:=0 to checks.count-1 do begin
    p:=pos('=',checks[i]);
    if p > 0 then begin
      if copy(checks[i],1,p-1) = str then begin
        strfound:=true;
        break;
      end
      else if copy(checks[i],p+1,9999) = val then begin
        valfound:=true;
        break;
      end;
    end;
  end;
  checks.free;
  if strfound then begin
    messagedlg('A String of "'+str+'" already exists',
      mtinformation,[mbok],0);
    EditString.selectall;
    EditString.setfocus;
    exit;
  end
  else if valfound then begin
    messagedlg('A Value of "'+val+'" already exists',
      mtinformation,[mbok],0);
    EditValue.selectall;
    EditValue.setfocus;
    exit;
  end;
  List1.items.add(str+'='+val);
  EditString.setfocus;
end;

end.
